﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HouseHoldingRunEngineTests.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the HouseHoldingRunEngineTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace BusinessEngineTests
{
  using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using BusinessEngineInterfaces;
  using BusinessEngines;
  using Entities;
  using FileRepository.Interfaces;
  using Moq;
  using NUnit.Framework;
  using ServiceInterfaces;
  using UnityRepository.Interfaces;

  [TestFixture]
  public class HouseHoldingRunEngineTests
  {
    private Mock<IHouseHoldingFileRepository> houseHoldingFileRepository;
    private Mock<IHouseHoldingRunService> houseHoldingRunService;
    private Mock<IDocumentService> documentService;
    private Mock<IGridRunService> gridRunService;
    private IHouseHoldingRunEngine houseHoldingRunEngine;

    [SetUp]
    public void SetUp()
    {
      this.houseHoldingFileRepository = new Mock<IHouseHoldingFileRepository>();
      this.houseHoldingRunService = new Mock<IHouseHoldingRunService>();
      this.documentService = new Mock<IDocumentService>();
      this.gridRunService = new Mock<IGridRunService>();
      this.houseHoldingRunEngine = new HouseHoldingRunEngine(
        this.houseHoldingFileRepository.Object,
        this.houseHoldingRunService.Object,
        this.documentService.Object,
        this.gridRunService.Object);
    }

    [Test]
    public void GivenValidData_WhenITryToHouseHold_HouseHoldInformationIsSaved()
    {
      this.houseHoldingFileRepository.Setup(h => h.GetHouseHoldingData())
        .Returns(
          new HouseHoldingRunData
            {
              Grid = "grid",
              ProcessingGridRun = new Collection<String> { "grid1", "grid2" },
              DocumentRunData =
                new Collection<DocumentRunData>
                  {
                    new DocumentRunData
                      {
                        DocumentId =
                          Guid.NewGuid()
                          .ToString(),
                        HouseHoldDate =
                          DateTime.Now
                      },
                    new DocumentRunData
                      {
                        DocumentId =
                          Guid.NewGuid()
                          .ToString(),
                        HouseHoldDate =
                          DateTime.Now
                      }
                  }
            });

      this.houseHoldingRunService.Setup(
        hhrs => hhrs.CreateHouseHoldingRun(It.IsAny<String>(), It.IsAny<DateTime>(), It.IsAny<DateTime>()))
        .Returns(new HouseHoldingRun());

      this.documentService.Setup(ds => ds.GetDocuments(It.IsAny<IEnumerable<String>>()))
        .Returns(new[]
        {
          new Document(), 
          new Document()
        });

      this.houseHoldingRunEngine.ProcessHouseHoldingRun();

      this.houseHoldingRunService.Verify(
        hhrs => hhrs.CreateHouseHoldingRun(It.IsAny<String>(), It.IsAny<DateTime>(), It.IsAny<DateTime>()));

      this.gridRunService.Verify(grs => grs.UpdateAsHouseheld(It.IsAny<String>(), It.IsAny<Int32>()), Times.Exactly(2));
      this.documentService.Verify(ds => ds.GetDocuments(It.IsAny<IEnumerable<String>>()), Times.Once);
      this.documentService.Verify(x => x.UpdateAsHouseheld(It.IsAny<IEnumerable<Int32>>(), It.IsAny<Int32>()), Times.Once);
    }
  }
}
